#include "dev/inc/dev_data_process.h"
#include <QByteArray>
#include "dev/inc/dev_debug.h"
#include <QMessageBox>

//QByteArray DEV_INT_To_Hex(int dec)
//{
//    //保证数据在两帧范围内
//        if(dec > 65535)
//        {
//            QMessageBox::information(NULL, "警告", "输入超出规定范围（input < 65535）");
//            return "0";
//        }
//        QString hexStr;
//        int temp = dec / 16;
//        int left = dec % 16;
//        if(temp > 0)
//            hexStr += decInt2HexStr(temp);
//        if(left < 10)
//            hexStr += (left + '0');
//        else
//            hexStr += ('A' + left - 10);
//        return hexStr;
//}
char DEV_Conver_Hex_To_Char(char c)
{
    if((c >= '0') && (c <= '9'))
        return c - 0x30;
    else if((c >= 'A') && (c <= 'F'))
        return c - 'A' + 10;//'A' = 65;
    else if((c >= 'a') && (c <= 'f'))
        return c - 'a' + 10;
    else
        return -1;
}
QByteArray DEV_Qstring_To_Hex(QString hexStr)
{
    QByteArray senddata;
    int hexdata, lowhexdata;
    int hexdatalen = 0;
    int len = hexStr.length();
    senddata.resize(len/2);
    char lstr, hstr;
    for(int i = 0; i < len; )
    {
        //将第一个不为' '的字符赋给hstr;
        hstr = hexStr[i].toLatin1();
        if(hstr == ' ')
        {
            i++;
            continue;
        }
        i++;
        //当i >= len时，跳出循环
        if(i >= len)
            break;
        //当i < len时，将下一个字符赋值给lstr;
        lstr = hexStr[i].toLatin1();
        //将hstr和lstr转换为0-15的对应数值
        hexdata = DEV_Conver_Hex_To_Char(hstr);
        lowhexdata = DEV_Conver_Hex_To_Char(lstr);
        //
        if((hexdata == 16) || (lowhexdata == 16))
            break;
        else
            hexdata = hexdata * 16 + lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
    return senddata;
}
